package top.fzqblog.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.fzqblog.Utils.R;
import top.fzqblog.Utils.SaiZiUtil;
import top.fzqblog.Utils.StringUtil;
import top.fzqblog.model.Param;
import top.fzqblog.model.User;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by 抽离 on 2017/9/1 0001.
 */
@Controller
public class UserAction extends BaseAction{

    @RequestMapping("/")
    public String index(HttpSession session){
        if(getCurrentUser(session) != null){
            return "/main/index";
        }
        return "login";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    @ResponseBody
    @RequestMapping("checkLogin")
    public R checkLogin(HttpSession session, User user){
        User u = new User().selectOne(new EntityWrapper<User>(user).eq("user_type", 1));
        if(user == null || u == null){
            return R.error("账号或密码错误！");
        }
        session.setAttribute("user", u);
        return R.ok();
    }


    @RequestMapping("userList")
    public String userList(HttpSession session){
        if(getCurrentUser(session) != null){
            return "/system/user_list";
        }
        return "login";
    }

    @RequestMapping("userEdit")
    public String userEdit(HttpSession session, Model model, Integer userId){
        System.out.println("userId：" + userId);
        if(userId != null){
            model.addAttribute("user", new User().selectById(userId));
        }
        if(getCurrentUser(session) != null){
            return "/system/user_edit";
        }
        return "login";
    }


    @ResponseBody
    @RequestMapping("userUpdate")
    public R userUpdate(HttpSession session, User user){
        if(getCurrentUser(session) == null){
            return R.error("请先登录！");
        }
        if(user !=null && user.getUserId() != null){
            user.updateById();
        }
        return R.ok();
    }

    @ResponseBody
    @RequestMapping("getUserList")
    public R getUserList(HttpSession session, String userName, String tel,  int page, int limit){
        if(getCurrentUser(session) == null){
            return R.error();
        }
        Page<User> p = new Page<User>();
        p.setCurrent(page);
        p.setSize(limit);
        Page<User> userPage = new User().selectPage(p, new EntityWrapper<User>().like("user_name", userName).like("tel", tel).orderBy("create_time", false));
        return R.ok().put("count", userPage.getTotal()).put("data", userPage.getRecords());
    }


    @RequestMapping("wxLogin")
    public String wxLogin(){
        return "/wx/login";
    }

    @RequestMapping("wxRegist")
    public String wxRegist(){
        return "/wx/regist";
    }

    @RequestMapping("findPwd")
    public String findPwd(){
        return "/wx/findpwd";
    }


    @ResponseBody
    @RequestMapping("wxLogin.do")
    public R wxLoginDo(HttpSession session, String userName, String password){
        if (StringUtil.isNotEmpty(userName) && userName.length() == 11){
            User user = new User().selectOne(new EntityWrapper<User>().eq("tel", userName)
                    .eq("password", password)
                    .eq("user_status", 1));
            if(user == null){
                return R.error("手机号或密码错误！");
            }
            session.setAttribute("wxUser", user);
        }else if(StringUtil.isNotEmpty(userName)){
            User user = new User().selectOne(new EntityWrapper<User>().eq("user_name", userName)
                    .eq("password", password)
                    .eq("user_status", 1));
            if(user == null) {
                return R.error("用户名或密码错误！");
            }
            session.setAttribute("wxUser", user);
        }
        else{
            return  R.error("参数错误！");
        }
        return R.ok();
    }

    @RequestMapping("wxIndex")
    public String wxIndex(HttpSession session, Model model){
//        if(checkWxLogin(session)){
//            return "/wx/index";
//        }
//        return "redirect:/wxLogin";
        User user = (User)getCurrentWxUser(session);
        model.addAttribute("user", user);
        Param param = new Param().selectOne(new EntityWrapper<Param>().eq("param_name", "dayJoinCount"));
        model.addAttribute("dayJoinCount", param.getParamValue());
        return "/wx/index";
    }


    @RequestMapping("paramSetting")
    public String paramSetting(HttpSession session, Model model){
        if(getCurrentUser(session) == null){
            return "login";
        }
        Param param = new Param().selectOne(new EntityWrapper<Param>().eq("param_name", "dayJoinCount"));
        model.addAttribute("dayJoinCount", param.getParamValue());
        return "/system/paramSetting";
    }


    @ResponseBody
    @RequestMapping("paramUpdate")
    public R paramUpdate(HttpSession session, Integer dayJoinCount){
        if(getCurrentUser(session) == null){
            return R.error("请先登录！ ");
        }
        Param param = new Param().selectOne(new EntityWrapper<Param>().eq("param_name", "dayJoinCount"));
        param.setParamValue(dayJoinCount + "");
        param.setCreateTime(new Date());
        param.updateById();
        return R.ok();
    }


    @ResponseBody
    @RequestMapping("bobing")
    public R BobingRun(HttpSession session){
        if(!checkWxLogin(session)){
            return R.error("请先登录！");
        }
        User user = getCurrentWxUser(session);
        System.out.println("user：" + user);
        Integer[] resultarr = SaiZiUtil.RandomSaiZi();
        String resultname = SaiZiUtil.getSaiZiType(resultarr);
        Integer credits = SaiZiUtil.getCredits(resultname);
        int count = user.getDayJoinCount();
        if(count <= 0){
            return R.error(100,"今日博饼次数已用光！");
        }
        int totalJoinCount = user.getTotalJoinCount();
        int userCredits = user.getCredits();
        user.setDayJoinCount(count-1);
        user.setCredits(userCredits + credits);
        user.setTotalJoinCount(totalJoinCount + 1);
        user.updateById();
        return R.ok()
                .put("resultarr",resultarr).put("resultname", resultname)
                .put("credits", credits).put("count",count-1).put("u_count", totalJoinCount + 1)
                .put("u_credits", userCredits + credits);
    }

    @ResponseBody
    @RequestMapping("canBobing")
    public R canBobing(HttpSession session){
        if(!checkWxLogin(session)){
            return R.error("请先登录！");
        }
        User user = getCurrentWxUser(session);
        if(user== null || user.getUserStatus() == 0){
            return R.error("用户不存在！");
        }
        Integer[] resultarr = SaiZiUtil.RandomSaiZi();
        String resultname = SaiZiUtil.getSaiZiType(resultarr);
        Integer credits = SaiZiUtil.getCredits(resultname);
        int count = user.getDayJoinCount();
        if(count <= 0){
            return R.error(100,"今日博饼次数已用光！");
        }
        return R.ok();
    }

    @RequestMapping("wxRegist.do")
    @ResponseBody
    public R regist(String tel, String password){
        User u = new User().selectOne(new EntityWrapper<User>().eq("tel", tel));
        if(u != null){
            return R.error("手机号码已注册！");
        }
        User user = new User();
        user.setCredits(0);
        user.setTotalJoinCount(0);
        user.setPassword(password);
        user.setTel(tel);
        user.setCreateTime(new Date());
        user.setUserStatus(1);
        Param param = new Param().selectOne(new EntityWrapper<Param>().eq("param_name", "dayJoinCount"));
        user.setDayJoinCount(Integer.parseInt(param.getParamValue()));
        user.insert();
        return R.ok();
    }
}
